/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于用户的一些业务逻辑操作
 * @author yanwl
 * @date 2015-03-20
 */
package com.company.example.controller;

import com.company.example.interceptor.MethodLog;
import com.company.example.model.pojo.User;
import com.company.example.service.UserService;
import com.company.example.service.UserServiceTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 控制器注解 请求映射注解 controller 代表类是控制器 restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * 
 * @author yanwl
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	// 每页显示的用户数
	private int pageSize = 10;
	// 用户列表页码
	private int currPage = 1;

	/**
	 * 注入 业务逻辑层
	 */
	@Resource
	private UserService userService;
	@Resource(name="userServiceTest")
	private UserServiceTest userServiceTest;
	
	/**
	 * @return 测试页面 
	 * @throws Exception
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public ModelAndView index() throws Exception {
		ModelAndView mav = new ModelAndView();
		User user = userServiceTest.findUserByName("李四");
		int rows = userServiceTest.selectTotalRows();
		System.out.println(user.getUserName()+"--"+user.getUserEmail()+"=="+rows);
		mav.setViewName("test");

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("field","user_id");
		map.put("value",1);
		User u = userService.selectUser(map);
		System.out.println(u.getUserName()+"--"+u.getUserEmail());

		return mav;
	}

	/**
	 * @return 用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value = "/test.htm/{id}", method = RequestMethod.GET)
	@ResponseBody
	@MethodLog(remark = "会员查询")
	public void test(@PathVariable Integer id,String name) throws Exception {
		for(int i = 3; i < 4; i++) {
			User entity = new User();
			entity.setUserEmail("745214429@qq.com");
			entity.setUserName("龙哥"+i);
			entity.setUserPassword("000000");
			userService.insertUser(entity);
//			userServiceTest.insert(entity);
		}
		
		
		/*List<User> users = userService.selectAllUsers();
		//List<User> users = userServiceTest.selectAllObjects();
		for (User user : users) {
			System.out.println(user.getUserName() + "--" + user.getUserEmail());
		}
		
		System.err.println("***********************************");
		
		User user = userServiceTest.selectByPrimaryKey(1);
		System.out.println(user.getUserName() + "--" + user.getUserEmail());*/

		/*Map<String,Object> map = new HashMap<String,Object>();
		map.put("field", "user_name");
		map.put("value", "张三");
		User user = userService.selectUser(map);
		System.out.println(user.getUserEmail()+"--"+user.getUserPassword());
		user.setUserName("张三1");
		userService.updateUser(user);*/

		//System.out.println(Globals.userName);
		
		//userServiceTest.delete(12);
		 
	}

	/**
	 * @return 企业列表页面
	 * @throws Exception
	 */
	@RequestMapping(value = "/userList.htm", method = RequestMethod.GET)
	public ModelAndView userList() throws Exception {
		ModelAndView mav = new ModelAndView();
		List<User> users = userService.selectAllUsers();
		mav.addObject("users", users);
		mav.setViewName("user_list");
		return mav;
	}
	
	

	/**
	 * @return 企业列表页面
	 * @throws Exception
	 */
	/*@RequestMapping(value = "/stuList.htm", method = RequestMethod.GET)
	public ModelAndView stuList() throws Exception {
		ModelAndView mav = new ModelAndView();
		List<User> users = userService.selectAllUsers();
		mav.addObject("users", users);
		mav.setViewName("stu_list");
		return mav;
	}*/

	/**
	 * Ajax 根据userID删除企业用户
	 * 
	 * @return 企业用户列表页面
	 * @throws Exception
	 */
	/*
	 * @RequestMapping(value="/operate/userAdmin/deleteCompany",method=RequestMethod
	 * .POST)
	 * 
	 * @ResponseBody public Map<String, Object> deleteCompany(HttpServletRequest
	 * request,Integer userId)throws Exception{
	 * 
	 * User user = userService.selectUserInfoByUserId(userId); boolean isSuccess
	 * = false;
	 * 
	 * Map<String, Object> map = new HashMap<String, Object>();
	 * map.put("userId", userId); List<Order> order =
	 * orderService.selectOrdersByMap(map);
	 * 
	 * if(order.size() != 0){ map.put("message", "用户有未完成订单，不能删除！！"); }else{
	 * isSuccess = userService.deleteUser(user); if(isSuccess) {
	 * map.put("message", "删除成功！！"); }else { map.put("message", "删除失败！！"); } }
	 * map.put("isSuccess", isSuccess); return map; }
	 */
}
